package com.linkage.educloud.ah.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import com.linkage.educloud.ah.R;
import com.zzhou.common.library.util.LogUtils;

import butterknife.ButterKnife;

/**
 * <desc>
 * Created by The Moss on 2018/6/7.
 */

public abstract class BasicTitleActivity extends AbstractActivity {

    View rootView;
    public Toolbar _mToolbar;
    AppBarLayout _mAppbarlayout;
    FrameLayout _mContentLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.d(TAG, TAG + " >>>> onCreate  <<<<");
//        getDelegate().setContentView(R.layout.basic_activity_title);
        rootView = View.inflate(this, R.layout.basic_title_activity,null);
        _mToolbar = rootView.findViewById(R.id._toolbar);
        _mAppbarlayout = rootView.findViewById(R.id._appbarlayout);
        _mContentLayout = rootView.findViewById(R.id._content);
        _mToolbar.setTitle("");
        setSupportActionBar(_mToolbar);
        setBackBar(true);
        addContentView(attachLayoutRes());
    }
    @Override
    public void setTitle(CharSequence title) {
        _mToolbar.setTitle(title);
    }

    @Override
    public void setTitle(int titleId) {
        _mToolbar.setTitle(titleId);
    }

    public void setSubtitle(CharSequence title) {
        _mToolbar.setSubtitle(title);
    }

    public void setSubtitle(int titleId) {
        _mToolbar.setSubtitle(titleId);
    }

    public void setSubtitleTextColor(int color) {
        _mToolbar.setSubtitleTextColor(color);
    }

    public void setTitleTextColor(int color) {
        _mToolbar.setTitleTextColor(color);
    }
    public void setTitleBg(int color){
        _mToolbar.setBackgroundResource(color);
    }

    public void addContentView(int layoutResID) {
        _mContentLayout.removeAllViews();
        if (layoutResID < 1) {
            LogUtils.e(TAG, "layout resouce id not exist");
            return;
        }
        LogUtils.d(TAG, ">>>>>>> setContentView");
        View view = View.inflate(this, attachLayoutRes(), null);
        if (view == null) {
            setContentView(rootView);
            return;
        }
        _mContentLayout.addView(view);
        setContentView(rootView);
        ButterKnife.bind(this,rootView);
    }

    public void setBackBar(boolean isShow) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(isShow);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return onOptionsItemSelectedCompat(item);
    }

    protected boolean onOptionsItemSelectedCompat(MenuItem item) {
        return false;
    }
}
